package com.zhy;

import java.util.HashMap;
import java.util.Map;

public class MapPracticeDemo {
    public static void main(String[] args) {
        Map<String,String> data = new HashMap<>();
        data.put("小王","A");
        data.put("小李","B");
        data.put("小王4","C");
        data.put("小王5","D");
        data.put("小王6","A");
        data.put("小王7","B");
        data.put("小王8","C");
        data.put("小李1","D");
        data.put("小李2","A");
        data.put("小王1","B");
        data.put("小王2","C");
        data.put("小王3","D");
        data.put("小李3","A");
        data.put("小李4","B");
        data.put("小李5","C");
        data.put("小王9","D");
        count(data);
    }
    public static void count(Map<String,String> data){
        /*1.创建一个Map集合，用来存储统计投票结果
         * 2.遍历data集合
         * 3.将value（A）拿出来，将value（A）当做key存储到新的集合中
         * 4.如果A已经存在*/
        Map<String,Integer> resultMap = new HashMap<>();
        data.forEach((k,v)->{
            boolean  containsKey = resultMap.containsKey(v);
            if (containsKey) {
                Integer  oldcount = resultMap.get(v);
                resultMap.put(v, oldcount + 1);
            }else {
                resultMap.put(v, 1);
            }

        });
        resultMap.forEach((k,v)->{
            System.out.println(k + "---" + v);
        });
    }
}
